package com.bdqn.wang.dao.impl;

import com.bdqn.wang.dao.OrderAllDao;
import com.bdqn.wang.pojo.OrderAll;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderAllDaoImpl extends BaseDao implements OrderAllDao {

    @Override
    public List<OrderAll> getAll() {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            connection = getConnection();
            String sql = "SELECT\n" +
                    "\torder_detail.id,\n" +
                    "\t_order.loginName,\n" +
                    "\t_product.NAME,\n" +
                    "\t_product.price,\n" +
                    "\torder_detail.quantity,\n" +
                    "\torder_detail.cost,\n" +
                    "\t_order.serialNumber\n" +
                    "FROM\n" +
                    "\teasybuy_order _order\n" +
                    "\tINNER JOIN easybuy_order_detail order_detail ON _order.id = order_detail.orderId\n" +
                    "\tINNER JOIN easybuy_product _product ON _product.id = order_detail.productid\n" +
                    "WHERE order_detail.shanchu=0";
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();

            //从结果集中取出数据，存储到集合当中
            //1.定义集合
            List<OrderAll> orderList = new ArrayList<OrderAll>();
            //2.遍历结果集
            while (resultSet.next()){
                OrderAll orderAll = new OrderAll(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5),
                        resultSet.getInt(6),
                        resultSet.getString(7)
                );
                orderList.add(orderAll);
            }
            //5.返回集合
            return orderList;
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,ps,connection);
        }
        return null;
    }

    @Override
    public int add(OrderAll orderAll) {
        return 0;
    }

    @Override
    public int del(int id) {
        String sql="update easybuy_order_detail set shanchu = 1 where id = ?";
        Object [] obj = {id};
        return executeUpdate(sql,obj);
    }

    @Override
    public int update(OrderAll orderAll) {
        String sql="";
        Object [] obj = {

                orderAll.getCost(),

                orderAll.getId()
        };
        return executeUpdate(sql,obj);
    }

    @Override
    public OrderAll getOrderByid(int id) {
        return null;
    }

    @Override
    public OrderAll getOrderXiang(int xiang) {
        return null;
    }
}
